package com.tarena.lbs.basic.web.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.basic.web.mapper.AdminMapper;
import com.tarena.lbs.pojo.basic.param.AdminParam;
import com.tarena.lbs.pojo.basic.po.AdminPO;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AdminRepository {
    @Autowired//如果你的idea发现mapper的注入有问题 idea校验力度比较强,实际不一定有错误
    private AdminMapper adminMapper;//mybatis-plus的实现

    public AdminPO getAdminByPhone(String phone) {
        //select * from lbs_admin where account_phone=#{phone};
        QueryWrapper<AdminPO> query=new QueryWrapper<>();
        query.eq("account_phone",phone);
        return adminMapper.selectOne(query);
    }

    public AdminPO getAdminById(Integer adminId) {
        return adminMapper.selectById(adminId);
    }

    public PageInfo<AdminPO> getAdminByPage(AdminQuery query) {
        //1.mybatis-plus查询条件 课下补充2个条件 limit 0,10
        //QueryWrapper<AdminPO> queryWrapper = new QueryWrapper<>();
        //2.分页插件
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<AdminPO> pos = adminMapper.selectList(null);
        return new PageInfo<>(pos);
    }
    public Long countByPhoneOrEmail(AdminParam param) {
        //where phone=#{phone} or email=#{email}
        QueryWrapper queryWrapper=new QueryWrapper();
        if (StringUtils.isNotBlank(param.getAccountPhone())&&StringUtils.isNotBlank(param.getEmail())){
            queryWrapper.eq("account_phone",param.getAccountPhone());
            queryWrapper.or();
            queryWrapper.eq("email",param.getEmail());
        }else if (StringUtils.isNotBlank(param.getAccountPhone())){
            queryWrapper.eq("account_phone",param.getAccountPhone());
        }else if (StringUtils.isNotBlank(param.getEmail())){
            queryWrapper.eq("email",param.getEmail());
        }
        return adminMapper.selectCount(queryWrapper);
    }
    public void saveAdmin(AdminPO poParam) {
        adminMapper.insert(poParam);
    }
}
